  /*
  ******************************************************************************
  * @file    w25qxx.c
  * @brief   W25Qxx驱动文件
  *
  ******************************************************************************
  */
#include "w25qxx.h"
#include "sym32l010_hal_systick.h"




/**
  * @brief  Initializes the W25QXXXX interface.
  * @retval W25QXXXX memory status
  *         W25Qx_OK        ：成功
  *         W25Qx_ERROR     ：错误
  *         W25Qx_BUSY      ：状态忙
  *         W25Qx_TIMEOUT   ：超时
  */
uint8_t W25Qx_Init(void)
{
    /* Reset W25Qxxx */
    W25Qx_Reset();

    return W25Qx_GetStatus();
}

/**
  * @brief  This function reset the W25Qx.
  * @retval None
  */
static void W25Qx_Reset(void)
{
    uint8_t cmd[2] = {RESET_ENABLE_CMD,RESET_MEMORY_CMD};

    W25Qx_Enable();
    /* Send the reset command */
    HAL_SPI_Transmit(&hspi1, cmd, 2, W25QXXXX_TIMEOUT_VALUE);
    W25Qx_Disable();

}

/**
  * @brief  Reads current status of the W25QXXXX.
  * @retval W25QXXXX memory status
  *         W25Qx_OK        ：成功
  *         W25Qx_ERROR     ：错误
  *         W25Qx_BUSY      ：状态忙
  *         W25Qx_TIMEOUT   ：超时
  */
static uint8_t W25Qx_GetStatus(void)
{
    uint8_t cmd[] = {READ_STATUS_REG1_CMD};
    uint8_t status;

    W25Qx_Enable();

    /* Send the read status command */
    HAL_SPI_Transmit(&hspi1, cmd, 1, W25QXXXX_TIMEOUT_VALUE);
    /* Reception of the data */
    HAL_SPI_CLR_ALLFLAG(&hspi1);
    HAL_SPI_Receive(&hspi1, &status, 1, W25QXXXX_TIMEOUT_VALUE);
    W25Qx_Disable();

    /* Check the value of the register */
    if ((status & W25QXXXX_FSR_BUSY) != 0)
    {
        return W25Qx_BUSY;
    }
    else
    {
        return W25Qx_OK;
    }
}

/**
  * @brief  This function send a Write Enable and wait it is effective.
  * @retval W25QXXXX memory status
  *         W25Qx_OK        ：成功
  *         W25Qx_ERROR     ：错误
  *         W25Qx_BUSY      ：状态忙
  *         W25Qx_TIMEOUT   ：超时
  */
uint8_t W25Qx_WriteEnable(void)
{
    uint8_t cmd[] = {WRITE_ENABLE_CMD};
    uint32_t tickstart = get_tick();

    /*Select the FLASH: Chip Select low */
    W25Qx_Enable();
    /* Send the read ID command */
    HAL_SPI_Transmit(&hspi1, cmd, 1, W25QXXXX_TIMEOUT_VALUE);
    /*Deselect the FLASH: Chip Select high */
    W25Qx_Disable();

    /* Wait the end of Flash writing */
    while (W25Qx_GetStatus() == W25Qx_BUSY)
    {
        /* Check for the Timeout */
        if ((get_tick() - tickstart) > W25QXXXX_TIMEOUT_VALUE)
        {
            return W25Qx_TIMEOUT;
        }
    }

    return W25Qx_OK;
}

/**
  * @brief  Read Manufacture/Device ID.
  * @retval value address
  *         0XEF13,表示芯片型号为W25Q80
  *         0XEF14,表示芯片型号为W25Q16
  *         0XEF15,表示芯片型号为W25Q32
  *         0XEF16,表示芯片型号为W25Q64
  */
uint16_t W25Qx_Read_ID(void)
{
    uint8_t idt[2];

    uint8_t cmd[4] = {READ_ID_CMD,0x00,0x00,0x00};

    W25Qx_Enable();
    /* Send the read ID command */
    HAL_SPI_Transmit(&hspi1, cmd, 4, W25QXXXX_TIMEOUT_VALUE);
    /* Reception of the data */
    HAL_SPI_CLR_ALLFLAG(&hspi1);
    HAL_SPI_Receive(&hspi1,idt, 2, W25QXXXX_TIMEOUT_VALUE);

    W25Qx_Disable();
    return ((idt[0] << 8) + idt[1]);
}

/**
  * @brief  Reads an amount of data from the QSPI memory.
  * @param  pData: Pointer to data to be read
  * @param  ReadAddr: Read start address
  * @param  Size: Size of data to read
  * @retval QSPI memory status
  */
uint8_t W25Qx_Read(uint8_t* pData, uint32_t ReadAddr, uint32_t Size)
{
    uint8_t cmd[4];

    /* Configure the command */
    cmd[0] = READ_CMD;
    cmd[1] = (uint8_t)(ReadAddr >> 16);
    cmd[2] = (uint8_t)(ReadAddr >> 8);
    cmd[3] = (uint8_t)(ReadAddr);

    W25Qx_Enable();
    /* Send the read ID command */
    HAL_SPI_Transmit(&hspi1, cmd, 4, W25QXXXX_TIMEOUT_VALUE);
    /* Reception of the data */
    HAL_SPI_CLR_ALLFLAG(&hspi1);
    if (HAL_SPI_Receive(&hspi1, pData, Size, W25QXXXX_TIMEOUT_VALUE) != HAL_OK)
    {
        return W25Qx_ERROR;
    }
    W25Qx_Disable();
    return W25Qx_OK;
}

/**
  * @brief  Writes an amount of data to the QSPI memory.
  * @param  pData: Pointer to data to be written
  * @param  WriteAddr: Write start address
  * @param  Size: Size of data to write,No more than 256byte.
  * @retval QSPI memory status
  */
uint8_t W25Qx_Write(uint8_t* pData, uint32_t WriteAddr, uint32_t Size)
{
    uint8_t cmd[4];
    uint32_t end_addr, current_size, current_addr;
    uint32_t tickstart = get_tick();

    /* Calculation of the size between the write address and the end of the page */
    current_addr = 0;

    while (current_addr <= WriteAddr)
    {
        current_addr += W25QXXXX_PAGE_SIZE;
    }
    current_size = current_addr - WriteAddr;

    /* Check if the size of the data is less than the remaining place in the page */
    if (current_size > Size)
    {
        current_size = Size;
    }

    /* Initialize the adress variables */
    current_addr = WriteAddr;
    end_addr = WriteAddr + Size;

    /* Perform the write page by page */
    do
    {
        /* Configure the command */
        cmd[0] = PAGE_PROG_CMD;
        cmd[1] = (uint8_t)(current_addr >> 16);
        cmd[2] = (uint8_t)(current_addr >> 8);
        cmd[3] = (uint8_t)(current_addr);

        /* Enable write operations */
        W25Qx_WriteEnable();

        W25Qx_Enable();
        /* Send the command */
        if (HAL_SPI_Transmit(&hspi1, cmd, 4, W25QXXXX_TIMEOUT_VALUE) != HAL_OK)
        {
            return W25Qx_ERROR;
        }

        /* Transmission of the data */
        if (HAL_SPI_Transmit(&hspi1, pData, current_size, W25QXXXX_TIMEOUT_VALUE) != HAL_OK)
        {
            return W25Qx_ERROR;
        }
        W25Qx_Disable();
        /* Wait the end of Flash writing */
        while (W25Qx_GetStatus() == W25Qx_BUSY)
        {
            /* Check for the Timeout */
            if ((get_tick() - tickstart) > W25QXXXX_TIMEOUT_VALUE)
            {
                return W25Qx_TIMEOUT;
            }
        }

        /* Update the address and size variables for next page programming */
        current_addr += current_size;
        pData += current_size;
        current_size = ((current_addr + W25QXXXX_PAGE_SIZE) > end_addr) ? (end_addr - current_addr) : W25QXXXX_PAGE_SIZE;
    } while (current_addr < end_addr);


    return W25Qx_OK;
}

/**
  * @brief  Erases the specified block of the QSPI memory.
  * @param  BlockAddress: Block address to erase
  * @retval QSPI memory status
  */
uint8_t W25Qx_Erase_Block(uint32_t Address)
{
    uint8_t cmd[4];
    uint32_t tickstart = get_tick();
    cmd[0] = SECTOR_ERASE_CMD;
    cmd[1] = (uint8_t)(Address >> 16);
    cmd[2] = (uint8_t)(Address >> 8);
    cmd[3] = (uint8_t)(Address);

    /* Enable write operations */
    W25Qx_WriteEnable();

    /*Select the FLASH: Chip Select low */
    W25Qx_Enable();
    /* Send the read ID command */
    HAL_SPI_Transmit(&hspi1, cmd, 4, W25QXXXX_TIMEOUT_VALUE);
    /*Deselect the FLASH: Chip Select high */
    W25Qx_Disable();

    /* Wait the end of Flash writing */
    while (W25Qx_GetStatus() == W25Qx_BUSY)
    {
        /* Check for the Timeout */
        if ((get_tick() - tickstart) > W25QXXXX_SECTOR_ERASE_MAX_TIME)
        {
            return W25Qx_TIMEOUT;
        }
    }
    return W25Qx_OK;
}

/**
  * @brief  Erases the entire QSPI memory.This function will take a very long time.
  * @retval QSPI memory status
  */
uint8_t W25Qx_Erase_Chip(void)
{
    uint8_t cmd[4];
    uint32_t tickstart = get_tick();
    cmd[0] = CHIP_ERASE_CMD;

    /* Enable write operations */
    W25Qx_WriteEnable();

    /*Select the FLASH: Chip Select low */
    W25Qx_Enable();
    /* Send the read ID command */
    HAL_SPI_Transmit(&hspi1, cmd, 1, W25QXXXX_TIMEOUT_VALUE);
    /*Deselect the FLASH: Chip Select high */
    W25Qx_Disable();

    /* Wait the end of Flash writing */
    while (W25Qx_GetStatus() == W25Qx_BUSY)
    {
        /* Check for the Timeout */
        if ((get_tick() - tickstart) > W25QXXXX_BULK_ERASE_MAX_TIME)
        {
            return W25Qx_TIMEOUT;
        }
    }
    return W25Qx_OK;
}


